package com.tengju.bff.interfaces.app.domination;

import com.tengju.bff.interfaces.app.domination.facade.DominationTeamFacade;
import com.tengju.bff.interfaces.app.domination.vo.*;
import com.tengju.bff.interfaces.app.domination.vo.CustomDominationTeamForm;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Api(tags = "app管辖团队接口")
@RequestMapping(value = "domination/team")
public class DominationTeamController {

    @Autowired
    DominationTeamFacade dominationTeamFacade;

    @ApiOperation(value = "管辖客户数", notes = "管辖客户数")
    @RequestMapping(value = "member/quantity", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<DominationMemberNumVO> memberQuantity() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        com.tengju.user.domain.model.user.UserIdCode userIdCode = servletContext.needIdCode();
        return ApiResponse.newSuccess(dominationTeamFacade.memberQuantity(new UserIdCode(userIdCode.getValue())));
    }

    @ApiOperation(value = "管辖客户列表", notes = "管辖客户列表")
    @RequestMapping(value = "member/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<DominationMemberListVO> memberList(@RequestBody DominationMemberListForm listForm) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        com.tengju.user.domain.model.user.UserIdCode userIdCode = servletContext.needIdCode();
        return ApiResponse.newSuccess(dominationTeamFacade.memberList(listForm,userIdCode));
    }


    @ApiOperation(value = "客户管辖团队列表", notes = "客户管辖团队列表")
    @RequestMapping(value = "customMember/domination/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<DominationMemberListVO> customMemberDominationList(@RequestBody CustomDominationTeamForm form) {
        try {
            DominationMemberListForm dominationMemberListForm = new DominationMemberListForm();
            dominationMemberListForm.setStarLevel(form.getStarLevel());
            dominationMemberListForm.setPageIndex(form.getPageIndex());
            dominationMemberListForm.setPageSize(form.getPageSize());
            dominationMemberListForm.setSortType(form.getSortType());
            return ApiResponse.newSuccess(dominationTeamFacade.memberList(dominationMemberListForm
                    ,new com.tengju.user.domain.model.user.UserIdCode(form.getIdCode())));
        } catch (InterfaceException | DomainException | ApplicationException e) {
            log.error("客户管辖团队列表获取失败：", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "本月新增客户数", notes = "本月新增客户数")
    @RequestMapping(value = "member/new/currentMonth/num", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<NewMemberInfoNumVO> memberNewNum() {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            com.tengju.user.domain.model.user.UserIdCode userIdCode = servletContext.needIdCode();
            return ApiResponse.newSuccess(dominationTeamFacade.memberNewNum(new UserIdCode(userIdCode.getValue())));
        } catch (InterfaceException | DomainException | ApplicationException e) {
            log.error("获取本月新增客户数失败：", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

    @ApiOperation(value = "本月新增客户数列表", notes = "本月新增客户数列表")
    @RequestMapping(value = "member/new/currentMonth/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<NewMemberDetailListVO> memberNewList(@RequestBody NewMemberDetailListForm form) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            com.tengju.user.domain.model.user.UserIdCode userIdCode = servletContext.needIdCode();
            return ApiResponse.newSuccess(dominationTeamFacade.newMemberList(form,new UserIdCode(userIdCode.getValue())));
        } catch (InterfaceException | DomainException | ApplicationException e) {
            log.error("获取本月新增客户数失败：", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "即将晋升客户数", notes = "即将晋升客户数")
    @RequestMapping(value = "member/upComing/num", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<MemberUpcomingNumVO> memberUpComingNum() {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            com.tengju.user.domain.model.user.UserIdCode userIdCode = servletContext.needIdCode();
            return ApiResponse.newSuccess(dominationTeamFacade.upcomingMemberNum(new UserIdCode(userIdCode.getValue())));
        } catch (InterfaceException | DomainException | ApplicationException e) {
            log.error("即将晋升客户数获取失败：", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "即将晋升客户数列表", notes = "即将晋升客户数列表")
    @RequestMapping(value = "member/upComing/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<MemberUpcomingListVO> memberUpComingList(@RequestBody MemberUpcomingListForm form) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            com.tengju.user.domain.model.user.UserIdCode userIdCode = servletContext.needIdCode();
            return ApiResponse.newSuccess(dominationTeamFacade.upcomingMemberList(form,new UserIdCode(userIdCode.getValue())));
        } catch (InterfaceException | DomainException | ApplicationException e) {
            log.error("即将晋升客户数列表失败：", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }


    @ApiOperation(value = "管辖客户备注", notes = "管辖客户备注")
    @RequestMapping(value = "member/remark/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> editRemark(@RequestBody DominationMemberNoteForm form) {

        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            com.tengju.user.domain.model.user.UserIdCode userIdCode = servletContext.needIdCode();
            return ApiResponse.newSuccess(dominationTeamFacade.editRemark(userIdCode,
                    new com.tengju.user.domain.model.user.UserIdCode(form.getSubordinateId()),form.getRemark()));
        } catch (InterfaceException | DomainException | ApplicationException e) {
            log.error("管辖客户备注失败：", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }
}
